package Temp.杂乱无章;

import java.util.*;

public class 六角填数 {
    public static void main(String[] args) {
        int n[] = new int[12];
        n[0] = 1;
        n[1] = 8;
        n[2] = 3;
        boolean n1[] = new boolean[13];
        n1[1] = n1[8] = n1[3] = true; //boolean判断这三个数

        int n2[] = {2, 4, 5, 6, 7, 9, 10, 11, 12};

        Shu(3, n, n2, n1);
    }

    public static void Shu(int i, int n[], int n2[], boolean n1[]) {
        if (i == 12) {
            if (Check(n) == true) {
                System.out.println(n[6]);
                return;
            }
            return;

        }
        for (int j = 0; j < n2.length; j++) {
            if (n1[n2[j]] == true) {
                continue;
            }
            else if (n1[n2[j]] == false) { //这个数字还没填入过
                n[i] = n2[j];
                n1[n2[j]] = true;
            }
            Shu(i + 1, n, n2, n1);
            n1[n2[j]] = false;
        }
    }

    public static boolean Check(int n[]) {   //检查行数总和是否相等
        int sum[] = new int[6];
        sum[0] = n[0] + n[3] + n[6] + n[8];
        sum[1] = n[0] + n[4] + n[7] + n[11];
        sum[2] = n[1] + n[3] + n[4] + n[5];
        sum[3] = n[1] + n[6] + n[9] + n[2];
        sum[4] = n[5] + n[7] + n[10] + n[2];
        sum[5] = n[8] + n[9] + n[10] + n[11];
        Arrays.sort(sum);
        if (sum[0] == sum[5]) {
            return true;
        }
        return false;
    }
}

